package com.eascs.web.o2o.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.app.exception.ControllerException;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.doc.excel.RowWriteHandler;
import com.eascs.common.doc.excel.WriteCell;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.report.common.entity.ReportStatForm;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.o2o.web.common.BaseMasterViewController;
import com.eascs.web.o2o.web.entity.O2oPlatformRegional;
import com.eascs.web.o2o.web.form.LoanForProvinceForm;
import com.eascs.web.o2o.web.service.BuildDataService;
import com.eascs.web.o2o.web.service.BuildEmailService;
import com.eascs.web.o2o.web.service.BuildExcelService;
import com.eascs.web.o2o.web.service.ProvinceNameData;
import com.eascs.web.o2o.web.service.ReportStatService;
import com.eascs.web.o2o.web.util.BankRowWriteHandler;
import com.eascs.web.o2o.web.util.BrowserUtils;

@Controller
@RequestMapping("/report")
public class MonReportController extends BaseMasterViewController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	ReportStatService reportStatService;

	@Autowired
	BuildExcelService buildExcelService;

	@Autowired
	BuildEmailService buildEmailService;

	@Inject
	BuildDataService buildDataService;

	@Autowired
	ProvinceNameData provinceNameData;

	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "report");
	}

	@RequestMapping("/downMonth")
	public String monthDowanload(HttpServletResponse httpServletResponse, HttpServletRequest request,
	                             String report_data, String type) {
		if (StringUtil.isNotBlank(report_data)) {
			String           year  = report_data.substring(0, 4);
			String           month = report_data.substring(4);
			String           date  = year + "-" + month;
			SimpleDateFormat sdf   = new SimpleDateFormat("yyyy-MM");
			try {
				Date monthDate = sdf.parse(date);
				// 生成报表excel，获得路径
				String out = buildExcelService.monthDetailReport(monthDate);
				if (("excel").equals(type)) {
					// 下载excel
					downLoad(out, httpServletResponse, request);
				} else {
					// 发送邮件
					buildEmailService.job_sendMail();
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}

		return null;
	}

	/*
	 *O2O金融百亿大战竞赛奖励发放
	 */
	@RequestMapping("/downAwardReport")
	public String downAwardMonth(HttpServletResponse httpServletResponse, HttpServletRequest request,
	                             String report_data) throws ParseException, IOException {
		if (StringUtil.isNotBlank(report_data)) {
			//查询时间
			String           year      = report_data.substring(0, 4);
			String           month     = report_data.substring(4);
			String           date      = year + "-" + month;
			SimpleDateFormat sdf       = new SimpleDateFormat("yyyy-MM");
			Date             monthDate = sdf.parse(date);
			//基础数据
			String         fileName = "O2O金融百亿大战竞赛奖励发放" + "（" + year + "年" + month + "月）";
			List<Object[]> data     = buildDataService.getRewardData(monthDate);
			//各个组别的月度业绩标杆
			List<Object[]> month_award_a = buildDataService.getMonthRewardData(monthDate, "A");
			List<Object[]> month_award_b = buildDataService.getMonthRewardData(monthDate, "B");


			String userAgent = request.getHeader("USER-AGENT");
			fileName = BrowserUtils.encodeFileName(userAgent, fileName);

			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
			httpServletResponse.setContentType("application/octet-stream; charset=ISO-8859-1");            //生成excel
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(),
					MonReportController.class.getResourceAsStream("/excelTemplate/14.xls"), new RowWriteHandler() {
						String titleStr = "O2O金融百亿大战竞赛奖励发放" + "（" + year + "年" + month + "月）";
						List<String> title = Arrays.asList(titleStr);
						int rowIndex = 0;//首个数据行
						BigDecimal group_month_award_a = new BigDecimal(0);
						BigDecimal group_month_award_b = new BigDecimal(0);
						BigDecimal group_month_award_ab = new BigDecimal(0);

						//开始的sheet
						@Override
						public int getSheetIndex() {
							return 0;
						}

						//开始的行
						@Override
						public int getRowIndex() {
							return 3;
						}

						//开始的列
						@Override
						public int getColumnIndex() {
							return 0;
						}

						//会展示的列
						@Override
						public int getColumnNumber() {
							return 16;
						}

						//下一行显示的数据
						@Override
						public Object[] nextRowDatas() {
							if (rowIndex == data.size())
								return null;

							Object[] objs = data.get(rowIndex);
							//当前行数据
							String     current_gro       = (String) objs[16];
							String     current_pro       = (String) objs[0];
							BigDecimal month_reward      = new BigDecimal(0);//每个省区获“月度业绩标杆”的平台
							BigDecimal month_reward_line = (BigDecimal) objs[10];//每个平台的“月度业绩标杆”值
							if (month_reward_line != null && month_reward_line.compareTo(month_reward) > 0) {
								month_reward = month_reward_line;
							}
							//下一行数据
							rowIndex++;
							String next_gro = rowIndex == data.size() ? null : (String) data.get(rowIndex)[16];
							String next_pro = rowIndex == data.size() ? null : (String) data.get(rowIndex)[0];


							if (!current_pro.equals(next_pro)) {//如果下一行的省份和上一行不同，则一个省份遍历完，开始算合计。

								//1.省区金融团队合计
								Object[]       objsPro     = null;
								List<Object[]> objsProData = buildDataService.getRewardGegData(monthDate, current_pro);
								if (objsProData != null && objsProData.size() > 0) {
									objsPro = objsProData.get(0);
								}

								//2.大区合计
								Object[]       objsReg     = null;
								List<Object[]> objsRegData = buildDataService.getRewardProData(monthDate, current_pro);
								if (objsRegData != null && objsRegData.size() > 0) {
									objsReg = objsRegData.get(0);
								}
								//处理“月度业绩标杆”
								if (objsReg != null && objsPro != null) {
									String group    = (String) objsReg[0];//当前组别
									String regional = (String) objsReg[1];//当前大区
									if (group != null && ("A").equals(group) && month_award_a != null) {
										for (int i = 0; i < month_award_a.size(); i++) {
											Object[] o          = month_award_a.get(i);
											String   regional_a = (String) o[0];//循环的大区
											if (regional.equals(regional_a)) {
												objsReg[11] = month_reward.add((BigDecimal) o[3]);
												objsPro[11] = month_reward.add((BigDecimal) o[3]);
												objsReg[15] = ((BigDecimal) objsReg[11]).add((BigDecimal) objsReg[15]);
												group_month_award_a = group_month_award_a.add((BigDecimal) objsReg[11]);
											}
										}
									} else if (group != null && ("B").equals(group) && month_award_b != null) {
										for (int i = 0; i < month_award_b.size(); i++) {
											Object[] o          = month_award_b.get(i);
											String   regional_b = (String) o[0];
											if (regional.equals(regional_b)) {
												objsReg[11] = month_reward.add((BigDecimal) o[3]);
												objsPro[11] = month_reward.add((BigDecimal) o[3]);
												objsReg[15] = ((BigDecimal) objsReg[11]).add((BigDecimal) objsReg[15]);
												group_month_award_b = group_month_award_b.add((BigDecimal) objsReg[11]);
											}
										}
									}
								}

								month_reward = new BigDecimal(0);


								//3.组别合计
								if (!current_gro.equals(next_gro)) {//当前组别  和 下一个组别 不一致，开始合计
									Object[]       objsGrp     = null;
									List<Object[]> objsGrpData = buildDataService.getRewardGroData(monthDate, current_gro);
									if (objsGrpData != null && objsGrpData.size() > 0) {
										objsGrp = objsGrpData.get(0);
									}
									//处理“月度业绩标杆”
									if (objsGrp != null && ("A组").equals(objsGrp[1])) {
										objsGrp[11] = group_month_award_a;
										objsGrp[15] = group_month_award_a.add((BigDecimal) objsGrp[15]);
									} else if (objsGrp != null && ("B组").equals(objsGrp[1])) {
										objsGrp[11] = group_month_award_b;
										objsGrp[15] = group_month_award_b.add((BigDecimal) objsGrp[15]);
									}


									//4.最后一行数据之后的大合计
									if (next_pro != null) {

										if (current_gro.equals("B") && next_gro.equals("O")) {//A，B组合计
											Object[]       objsGrp_AB     = null;
											List<Object[]> objsGrp_ABData = buildDataService.getRewardGroData(monthDate, "X");
											if (objsGrp_ABData != null && objsGrp_ABData.size() > 0) {
												objsGrp_AB = objsGrp_ABData.get(0);
											}
											group_month_award_ab = group_month_award_b.add(group_month_award_a);
											objsGrp_AB[11] = group_month_award_ab;
											objsGrp_AB[15] = group_month_award_ab.add((BigDecimal) objsGrp_AB[15]);
											return new Object[]{objs, objsPro, objsReg, objsGrp, objsGrp_AB};
										}

										return new Object[]{objs, objsPro, objsReg, objsGrp};
									} else {
										Object[]       objsGrp_1     = null;
										List<Object[]> objsGrp_1Data = buildDataService.getRewardRTotalData(monthDate, "1");
										if (objsGrp_1Data != null && objsGrp_1Data.size() > 0) {
											objsGrp_1 = objsGrp_1Data.get(0);
										}
										Object[]       objsGrp_2     = null;
										List<Object[]> objsGrp_2Data = buildDataService.getRewardRTotalData(monthDate, "2");
										if (objsGrp_2Data != null && objsGrp_2Data.size() > 0) {
											objsGrp_2 = objsGrp_2Data.get(0);
										}
										Object[]       objsGrp_3     = null;
										List<Object[]> objsGrp_3Data = buildDataService.getRewardRTotalData(monthDate, "3");
										if (objsGrp_3Data != null && objsGrp_3Data.size() > 0) {
											objsGrp_3 = objsGrp_3Data.get(0);
										}
										Object[] objsTotal_1 = objsGrp_1;
										Object[] objsTotal_2 = objsGrp_2;
										Object[] objsTotal_3 = objsGrp_3;
										Object[] objsGrp_O   = null;
										//group_month_award_ab = group_month_award_b.add(group_month_award_a);
										objsTotal_1[11] = group_month_award_ab;
										objsTotal_1[15] = group_month_award_ab.add((BigDecimal) objsTotal_1[15]);
										objsTotal_3[11] = group_month_award_ab;
										objsTotal_3[15] = group_month_award_ab.add((BigDecimal) objsTotal_3[15]);
										if (current_gro.equals("B")) {//A,B组合计
											List<Object[]> objsGrp_ABData = buildDataService.getRewardGroData(monthDate, "X");
											if (objsGrp_ABData != null && objsGrp_ABData.size() > 0) {
												objsGrp_O = objsGrp_ABData.get(0);
											}
											objsGrp_O[11] = group_month_award_ab;
											objsGrp_O[15] = group_month_award_ab.add((BigDecimal) objsGrp_O[15]);
											return new Object[]{objs, objsPro, objsReg, objsGrp, objsGrp_O, objsTotal_1, objsTotal_2, objsTotal_3};
										}
										return new Object[]{objs, objsPro, objsReg, objsGrp, objsTotal_1, objsTotal_2, objsTotal_3};

									}

								}
								return new Object[]{objs, objsPro, objsReg};//（一次遍历，同时输出数据行objs和统计行objsPro,objsReg）

							}
							return objs;
						}


						//标题（多级）
						@Override
						public List<WriteCell> getWriteCells() {
							ArrayList<WriteCell> list = new ArrayList<WriteCell>();
							if (title != null && title.size() > 0) {
								for (int i = 0; i < title.size(); i++) {
									list.add(new WriteCell(i, 0, title.get(i)));
								}
							}
							return list;
						}

						//需要统计的列
						@Override
						public int[] getTotalCols() {
							return null;
						}

						@Override
						public String[] getHeaderCols() {
							return new String[0];
						}
					});

		}

		return null;
	}


	public static void main(String[] args) {
		String q = "11";
		if (!q.equals(null)) {
			q = "21";
			System.out.println(q);
		}


	}


	@RequestMapping("/downDay")
	public String dayDowanload(HttpServletResponse httpServletResponse, HttpServletRequest request,
	                           String report_data) {
		if (StringUtil.isNotBlank(report_data)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = sdf.parse(report_data);
				// 生成报表excel，获得路径
				String out = buildExcelService.dayDetailReport(date);
				downLoad(out, httpServletResponse, request);
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}

		return null;

	}


	public void downLoad(String filePath, HttpServletResponse httpServletResponse, HttpServletRequest request) {

		OutputStream os = null;
		try {
			// 获取下载文件的名称
			String[] fileName  = filePath.split("/");
			String   outfile   = fileName[fileName.length - 1];
			String   userAgent = request.getHeader("User-Agent");
			byte[]   bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			// 下载文件
			File f = new File(filePath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}

	}

	@RequestMapping(value = "/month")
	public String monthReport() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "monthReport");
		return "/report/month";
	}

	@RequestMapping(value = "/day")
	public String dayReport() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "dayReport");
		return "/report/day";
	}

	@RequestMapping(value = "/rewardReport")
	public String rewardReport() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "rewardReport");
		return "/report/reward";
	}

	// 根据平台查询报表
	@RequestMapping(value = "/reportStatByPlatform")
	public String ReportStatByPlatform(ReportStatForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "reportStatByPlatform");

		String o2ourl = "/reportRest/o2oPlatform";

		if (entity != null) {
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String province      = entity.getProvince() == null ? "" : entity.getProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		List<O2oPlatformRegional> proList = provinceNameData.getProvinceNameData();

		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		ViewContext.put("proList", proList);
		return "reportstat/platformStatList";
	}

	@RequestMapping(value = "/reportStatByOperator")
	// 根据经办人查询报表
	public String ReportStatByOperator(ReportStatForm entity) {

		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "reportStatByOperator");
		String o2ourl = "/reportRest/o2oOperator";

		if (entity != null) {
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String province      = entity.getProvince() == null ? "" : entity.getProvince();
			String platform      = entity.getPlatform() == null ? "" : entity.getPlatform();
			String ltd           = entity.getLtd() == null ? "" : entity.getLtd();
			String operatorName  = entity.getOperatorName() == null ? "" : entity.getOperatorName();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "platform", platform);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operatorName", operatorName);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}

		List<O2oPlatformRegional> proList = provinceNameData.getProvinceNameData();

		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		ViewContext.put("proList", proList);
		return "reportstat/operatorStatList";
	}

	// 信贷经理放款清单(添加在职状态和入职时间)
	@RequestMapping(value = "/reportStatByDimission")
	public String reportStatByDimission(ReportStatForm entity) {

		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "reportStatByOperator");
		String o2ourl = "/reportRest/o2oDimission";

		if (entity != null) {
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String joinDateStart   = entity.getLoanDateEnd() == null ? null : entity.getJoinDateStart();
			String joinDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getJoinDateEnd();
			String province      = entity.getProvince() == null ? "" : entity.getProvince();
			String platform      = entity.getPlatform() == null ? "" : entity.getPlatform();
			String ltd           = entity.getLtd() == null ? "" : entity.getLtd();
			String isDimission           = entity.getIsDimission() == null ? "" : entity.getIsDimission();
			String operatorName  = entity.getOperatorName() == null ? "" : entity.getOperatorName();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "joinDateStart", joinDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "joinDateEnd", joinDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "platform", platform);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "isDimission", isDimission);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operatorName", operatorName);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}

		List<O2oPlatformRegional> proList = provinceNameData.getProvinceNameData();

		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		ViewContext.put("proList", proList);
		return "reportstat/creditManagerStatList";
	}

	@RequestMapping(value = "/reportStatByBankreFund")
	// 根据放款银行查询报表
	public String ReportStatByBankreFund(ReportStatForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "reportStatByBankreFund");
		String o2ourl = "/reportRest/o2oBankreFund";

		if (entity != null) {
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		return "reportstat/bankreFundStatList";
	}

	@RequestMapping(value = "/reportStatByProvince")
	// 根据省区查询报表
	public String ReportStatByProvince(ReportStatForm entity) {

		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "reportStatByProvince");
		String o2ourl = "/reportRest/o2oProvince";

		if (entity != null) {
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String province      = entity.getProvince() == null ? "" : entity.getProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}

		List<O2oPlatformRegional> proList = provinceNameData.getProvinceNameData();

		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		ViewContext.put("proList", proList);
		return "reportstat/provinceStatList";
	}

	// 平台查询报表下载
	@RequestMapping("/downloadPlatform")
	public void downloadPlatform(ReportStatForm entity, HttpServletResponse httpServletResponse,
	                             HttpServletRequest request) {
		String dateStart = entity.getLoanDateStart();
		String dateEnd   = entity.getLoanDateEnd();
		String province  = entity.getProvince();
		entity.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = reportStatService.getReportStatByPlatformByPage(entity);
		saveReportStatByPlatform(pageResult.getData(), dateStart, dateEnd, province);
		String       path    = MonReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/reportstat.xls").toString();
		OutputStream os      = null;
		try {
			String outfile;
			if (province == null) {
				if (dateStart.equals(dateEnd)) {
					outfile = "新增业务统计-按平台-" + entity.getLoanDateStart() + ".xls";
				} else {
					outfile = "新增业务统计-按平台-" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + ".xls";
				}
			} else {
				if (dateStart.equals(dateEnd)) {
					outfile = entity.getProvince() + "-新增业务统计-按平台-" + entity.getLoanDateStart() + ".xls";
				} else {
					outfile = entity.getProvince() + "-新增业务统计-按平台-" + entity.getLoanDateStart() + "至"
							+ entity.getLoanDateEnd() + ".xls";
				}
			}
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	public String saveReportStatByPlatform(List<Object[]> data, String loanDateStart, String loanDateEnd,
	                                       String province) {
		String dateStart = loanDateStart;
		String dateEnd   = loanDateEnd;
		String pro       = province;
		// 模板
		String soucreFileName = "/excelTemplate/reportstat_platform.xls";
		// 输出路径
		String       path    = MonReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/reportstat.xls").toString();

		// 包含的列
		int col = 7;
		// 需要统计的列
		int[]  arr = {2, 3, 4, 5, 6};
		String title;
		if (pro == null) {
			if (dateStart.equals(dateEnd)) {
				title = "新增业务统计-" + dateStart + ".xls";
			} else {
				title = "新增业务统计-" + dateStart + "至" + dateEnd + ".xls";
			}
		} else {
			if (dateStart.equals(dateEnd)) {
				title = pro + "-业务新增数据统计报表-" + dateStart + ".xls";
			} else {
				title = pro + "-业务新增数据统计报表-" + dateStart + "至" + dateEnd + ".xls";
			}
		}
		// 开始的表格，行，列
		int sheetIndex  = 0;
		int rowIndex    = 2;
		int columnIndex = 0;
		BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList(title),
				sheetIndex, rowIndex, columnIndex);

		// 生产excel
		write(soucreFileName, outPath, bankRowWriteHandler);

		return path + "/" + outPath;
	}

	// 经办人查询报表下载
	@RequestMapping("/downloadOperator")
	public void downloadOperator(ReportStatForm entity, HttpServletResponse httpServletResponse,
	                             HttpServletRequest request) {
		String dateStart = entity.getLoanDateStart();
		String dateEnd   = entity.getLoanDateEnd();
		String province  = entity.getProvince();
		String platform  = entity.getPlatform();
		entity.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = reportStatService.getReportStatByOperatorByPage(entity);
		saveReportStatByOperator(pageResult.getData(), dateStart, dateEnd, province, platform);
		String       path    = MonReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/reportstatoperator.xls").toString();
		OutputStream os      = null;
		try {
			String outfile;
			if (province == null) {
				if (platform == null) {
					if (dateStart.equals(dateEnd)) {
						outfile = "新增业务统计-按经办人-" + entity.getLoanDateStart() + ".xls";
					} else {
						outfile = "新增业务统计-按经办人-" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + ".xls";
					}
				} else {
					if (dateStart.equals(dateEnd)) {
						outfile = "新增业务统计-按经办人-" + entity.getLoanDateStart() + "（平台：" + entity.getPlatform() + "）"
								+ ".xls";
					} else {
						outfile = "新增业务统计-按经办人-" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + "（平台："
								+ entity.getPlatform() + "）" + ".xls";
					}
				}
			} else {
				if (platform == null) {
					if (dateStart.equals(dateEnd)) {
						outfile = entity.getProvince() + "-新增业务统计-按经办人-" + entity.getLoanDateStart() + ".xls";
					} else {
						outfile = entity.getProvince() + "-新增业务统计-按经办人-" + entity.getLoanDateStart() + "至"
								+ entity.getLoanDateEnd() + ".xls";
					}
				} else {
					if (dateStart.equals(dateEnd)) {
						outfile = entity.getProvince() + "-新增业务统计-按经办人-" + entity.getLoanDateStart() + "（平台："
								+ entity.getPlatform() + "）" + ".xls";
					} else {
						outfile = entity.getProvince() + "-新增业务统计-按经办人-" + entity.getLoanDateStart() + "至"
								+ entity.getLoanDateEnd() + "（平台：" + entity.getPlatform() + "）" + ".xls";
					}
				}
			}
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	// 信贷经理放款清单(添加在职状态和入职时间)
	@RequestMapping("/downloadDimission")
	public void downloadDimission(ReportStatForm entity, HttpServletResponse httpServletResponse,
	                             HttpServletRequest request) {
		String dateStart = entity.getLoanDateStart();
		String dateEnd   = entity.getLoanDateEnd();
		String province  = entity.getProvince();
		String platform  = entity.getPlatform();
		entity.setPageSize(Integer.MAX_VALUE);

		PageResult<Object[]> pageResult = reportStatService.getReportStatByDimissionByPage(entity);
		saveReportStatByOperator(pageResult.getData(), dateStart, dateEnd, province, platform);
		String       path    = MonReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/reportstat_dimmison.xls").toString();
		OutputStream os      = null;
		try {
			String outfile;
			if (province == null) {
				if (platform == null) {
					if (dateStart.equals(dateEnd)) {
						outfile = "信贷经理放款清单-" + entity.getLoanDateStart() + ".xls";
					} else {
						outfile = "信贷经理放款清单-" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + ".xls";
					}
				} else {
					if (dateStart.equals(dateEnd)) {
						outfile = "信贷经理放款清单-" + entity.getLoanDateStart() + "（平台：" + entity.getPlatform() + "）"
								+ ".xls";
					} else {
						outfile = "信贷经理放款清单-" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + "（平台："
								+ entity.getPlatform() + "）" + ".xls";
					}
				}
			} else {
				if (platform == null) {
					if (dateStart.equals(dateEnd)) {
						outfile = entity.getProvince() + "-信贷经理放款清单-" + entity.getLoanDateStart() + ".xls";
					} else {
						outfile = entity.getProvince() + "-信贷经理放款清单-" + entity.getLoanDateStart() + "至"
								+ entity.getLoanDateEnd() + ".xls";
					}
				} else {
					if (dateStart.equals(dateEnd)) {
						outfile = entity.getProvince() + "-信贷经理放款清单-" + entity.getLoanDateStart() + "（平台："
								+ entity.getPlatform() + "）" + ".xls";
					} else {
						outfile = entity.getProvince() + "-信贷经理放款清单-" + entity.getLoanDateStart() + "至"
								+ entity.getLoanDateEnd() + "（平台：" + entity.getPlatform() + "）" + ".xls";
					}
				}
			}
			String innerTile = outfile;

			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists()) throw new RuntimeIOException("file not found");

			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");

			// 开始的表格，行，列
			int sheetIndex  = 0;
			int rowIndex    = 2;
			int columnIndex = 0;
			// 包含的列
			int col = 9;
			// 需要统计的列
			int[]  arr = {4, 5, 6, 7, 8};

			/*DefaultRowWriteHandler defaultRowWriteHandler = new DefaultRowWriteHandler(pageResult.getData());
			defaultRowWriteHandler.setRowIndex(2);*/

			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(pageResult.getData(), col, arr, Arrays.asList(new String(bytes, "UTF-8")),
					0, 2, 0);
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), MonReportController.class.getResourceAsStream("/excelTemplate/reportstat_dimmison.xls"), bankRowWriteHandler);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	// 先生成文件然后在读这个文件(经办人查询报表)
	public String saveReportStatByOperator(List<Object[]> data, String loanDateStart, String loanDateEnd,
	                                       String province, String platform) {
		String dateStart = loanDateStart;
		String dateEnd   = loanDateEnd;
		String pro       = province;
		String pla       = platform;
		// 模板
		String soucreFileName = "/excelTemplate/reportstat_operator.xls";
		// 输出路径
		StringBuffer     s    = new StringBuffer();
		String           path = MonReportController.class.getResource("/excelTemplate").getPath();
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM");

		String outPath = s.append(path).append("/reportstatoperator.xls").toString();

		// 包含的列
		int col = 7;
		// 需要统计的列
		int[]  arr = {2, 3, 4, 5, 6};
		String title;
		if (pro == null) {
			if (pla == null) {
				if (dateStart.equals(dateEnd)) {
					title = "新增业务统计-" + dateStart + ".xls";
				} else {
					title = "新增业务统计-" + dateStart + "至" + dateEnd + ".xls";
				}
			} else {
				if (dateStart.equals(dateEnd)) {
					title = "新增业务统计-" + dateStart + "（平台：" + pla + "）" + ".xls";
				} else {
					title = "新增业务统计-" + dateStart + "至" + dateEnd + "（平台：" + pla + "）" + ".xls";
				}
			}
		} else {
			if (pla == null) {
				if (dateStart.equals(dateEnd)) {
					title = pro + "-业务新增数据统计报表-" + dateStart + ".xls";
				} else {
					title = pro + "-业务新增数据统计报表-" + dateStart + "至" + dateEnd + ".xls";
				}
			} else {
				if (dateStart.equals(dateEnd)) {
					title = pro + "-业务新增数据统计报表-" + dateStart + "（平台：" + pla + "）" + ".xls";
				} else {
					title = pro + "-业务新增数据统计报表-" + dateStart + "至" + dateEnd + "（平台：" + pla + "）" + ".xls";
				}
			}
		}
		// 开始的表格，行，列
		int sheetIndex  = 0;
		int rowIndex    = 2;
		int columnIndex = 0;
		BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList(title),
				sheetIndex, rowIndex, columnIndex);

		// 生产excel
		write(soucreFileName, outPath, bankRowWriteHandler);

		return path + "/" + outPath;
	}

	// 放款银行查询报表下载
	@RequestMapping("/downloadBankreFund")
	public void downloadBankreFund(ReportStatForm entity, HttpServletResponse httpServletResponse,
	                               HttpServletRequest request) {
		String dateStart = entity.getLoanDateStart();
		String dateEnd   = entity.getLoanDateEnd();
		entity.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = reportStatService.getReportStatByBankreFundByPage(entity);
		saveReportStatByBankreFund(pageResult.getData(), dateStart, dateEnd);
		String       path    = MonReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/reportstatbankrefund.xls").toString();
		OutputStream os      = null;
		try {
			String outfile;
			if (dateStart.equals(dateEnd)) {
				outfile = "新增业务统计-按放款银行-" + entity.getLoanDateStart() + ".xls";
			} else {
				outfile = "新增业务统计-按放款银行-" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + ".xls";
			}
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	// 先生成文件然后在读这个文件(放款银行查询报表)
	public String saveReportStatByBankreFund(List<Object[]> data, String loanDateStart, String loanDateEnd) {
		// 模板
		String soucreFileName = "/excelTemplate/reportstat_bankrefund.xls";
		// 输出路径
		StringBuffer     s    = new StringBuffer();
		String           path = MonReportController.class.getResource("/excelTemplate").getPath();
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM");

		String outPath = s.append(path).append("/reportstatbankrefund.xls").toString();

		// 包含的列
		int col = 6;
		// 需要统计的列
		int[]  arr   = {1, 2, 3, 4, 5};
		String title = loanDateStart + "至" + loanDateEnd + "业务新增数据统计报表";
		// 开始的表格，行，列
		int sheetIndex  = 0;
		int rowIndex    = 2;
		int columnIndex = 0;
		BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList(title),
				sheetIndex, rowIndex, columnIndex);

		// 生产excel
		write(soucreFileName, outPath, bankRowWriteHandler);

		return path + "/" + outPath;
	}

	// 省区查询报表下载
	@RequestMapping("/downloadProvince")
	public void downloadProvince(ReportStatForm entity, HttpServletResponse httpServletResponse,
	                             HttpServletRequest request) {
		String dateStart = entity.getLoanDateStart();
		String dateEnd   = entity.getLoanDateEnd();
		String province  = entity.getProvince();
		entity.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = reportStatService.getReportStatByProvinceByPage(entity);
		saveReportStatByProvince(pageResult.getData(), dateStart, dateEnd, province);
		String       path    = MonReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/reportstatprovince.xls").toString();
		OutputStream os      = null;
		try {
			String outfile;
			if (province == null) {
				if (dateStart.equals(dateEnd)) {
					outfile = "新增业务统计-按省区-" + entity.getLoanDateStart() + ".xls";
				} else {
					outfile = "新增业务统计-按省区-" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + ".xls";
				}
			} else {
				if (dateStart.equals(dateEnd)) {
					outfile = entity.getProvince() + "-新增业务统计-按省区-" + entity.getLoanDateStart() + ".xls";
				} else {
					outfile = entity.getProvince() + "-新增业务统计-按省区-" + entity.getLoanDateStart() + "至"
							+ entity.getLoanDateEnd() + ".xls";
				}
			}
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	// 先生成文件然后在读这个文件(省区查询报表)
	public String saveReportStatByProvince(List<Object[]> data, String loanDateStart, String loanDateEnd,
	                                       String province) {
		String dateStart = loanDateStart;
		String dateEnd   = loanDateEnd;
		String pro       = province;
		// 模板
		String soucreFileName = "/excelTemplate/reportstat_province.xls";
		// 输出路径
		StringBuffer     s    = new StringBuffer();
		String           path = MonReportController.class.getResource("/excelTemplate").getPath();
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM");

		String outPath = s.append(path).append("/reportstatprovince.xls").toString();

		// 包含的列
		int col = 6;
		// 需要统计的列
		int[]  arr = {1, 2, 3, 4, 5};
		String title;

		if (pro == null) {
			if (dateStart.equals(dateEnd)) {
				title = "新增业务统计-" + dateStart + ".xls";
			} else {
				title = "新增业务统计-" + dateStart + "至" + dateEnd + ".xls";
			}
		} else {
			if (dateStart.equals(dateEnd)) {
				title = pro + "-业务新增数据统计报表-" + dateStart + ".xls";
			} else {
				title = pro + "-业务新增数据统计报表-" + dateStart + "至" + dateEnd + ".xls";
			}
		}

		// 开始的表格，行，列
		int sheetIndex  = 0;
		int rowIndex    = 2;
		int columnIndex = 0;
		BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(data, col, arr, Arrays.asList(title),
				sheetIndex, rowIndex, columnIndex);

		// 生产excel
		write(soucreFileName, outPath, bankRowWriteHandler);

		return path + "/" + outPath;
	}

	/*
	 * 生成excel 参数：数据，源文件，输出文件，excel包含的列，需要统计的列，excel标题
	 */
	public void write(String soucreFileName, String outPath, BankRowWriteHandler bankRowWriteHandler) {
		IoUtil.createDirectorieIfMissing(new File(outPath).getParentFile());
		try {
			ExcelApiHelper.writeHandle(new FileOutputStream(outPath),
					BuildExcelService.class.getResourceAsStream(soucreFileName), bankRowWriteHandler);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ControllerException(e);
		}
	}
	
	
	/**
	 * 财务模块查询省区累计放款
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/searchLoanByProvince")
	public String searchLoanByProvince(LoanForProvinceForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "loanForProvince");
		String o2ourl = "/reportRest/searchLoanByProvince";
		if(entity != null) {
			String loanDateStart = entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd();
			String province = entity.getProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		List<O2oPlatformRegional> proList = provinceNameData.getProvinceNameData();

		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		ViewContext.put("proList", proList);
		return "finance/index";
	}
	
	
	@RequestMapping("/downloadLoanAmount")
	public void downloadLoanAmount(LoanForProvinceForm entity, HttpServletResponse httpServletResponse,
	                             HttpServletRequest request) {
		String dateStart = entity.getLoanDateStart();
		String dateEnd   = entity.getLoanDateEnd();
		String province  = entity.getProvince();
		entity.setPageSize(Integer.MAX_VALUE);

		PageResult<Object[]> pageResult = reportStatService.getLoanAmountByProvince(entity);
		String       path    = MonReportController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/loan-province.xls").toString();
		try {
			String outfile;
			if (province == null) {
				if (dateStart.equals(dateEnd)) {
					outfile = "各省区" + entity.getLoanDateStart() + "累计放款详情.xls";
				} else {
					outfile = "各省区" + entity.getLoanDateStart() + "至" + entity.getLoanDateEnd() + "累计放款详情.xls";
				}
			} else {
				if (dateStart.equals(dateEnd)) {
					outfile = entity.getProvince() + "省区" + entity.getLoanDateStart() + "累计放款详情.xls";
				} else {
					outfile = entity.getProvince() + "省区" + entity.getLoanDateStart() + "至"
							+ entity.getLoanDateEnd() + "累计放款详情.xls";
				}
			}

			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists()) throw new RuntimeIOException("file not found");

			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");

			// 开始的表格，行，列
			int sheetIndex  = 0;
			int rowIndex    = 2;
			int columnIndex = 0;
			// 包含的列
			int col = 2;
			// 需要统计的列
			int[]  arr = {1};


			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(pageResult.getData(), col, arr, Arrays.asList(new String(bytes, "UTF-8")),
					0, 2, 0);
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), MonReportController.class.getResourceAsStream("/excelTemplate/loan-province.xls"), bankRowWriteHandler);

		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} 
	}
	
	
	@RequestMapping(value = "/brandForProvince")
	public String brandForProvince() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "brandForProvinceReport");
		return "/report/brandForProvince";
	}
	
	
	@RequestMapping("/downProvinceBrand")
	public String brandForProvinceDownload(HttpServletResponse httpServletResponse, HttpServletRequest request, String queryDateStart, String queryDateEnd) {
		if (StringUtil.isNotBlank(queryDateStart) && StringUtil.isNotBlank(queryDateEnd)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date start = sdf.parse(queryDateStart);
				Date end = sdf.parse(queryDateEnd);
				// 生成报表excel，获得路径
				String out = buildExcelService.provinceBrandReport(start, end);
				downLoad(out, httpServletResponse, request);
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}

		return null;

	}
	
}
